<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	
 	include_once("includes/db_connect.php");
	$themesResult=mysql_query("SELECT t.type_id, ty.type, t.url FROM tbl_types ty, tbl_themes t  WHERE ty.theme_id=t.id AND ty.displayed='Y' ORDER BY ty.order");
	$showSplash = !isset($_SESSION['splashScreen']);
	$h = $_GET['h'];
	$w = $_GET['w'];
	$columnWidth = ($w/3)+($w/6);
	
	$showWeather = true;
	$showDate = true;
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PS3 Theatre <?php echo $_SESSION['version'];?></title>
<LINK href="css/main.css" rel="stylesheet" type="text/css">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/animator.js"></script>
<script type="text/javascript" src="js/time.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/index_ui.js"></script>
<script type="text/javascript">
var requestPending = false;
var loaderShowing = false;

var menuBarIndex = 0;
var menuBarItems = <?php echo mysql_num_rows($themesResult);?>;

<?php
if(isset($_GET['h']) && isset( $_GET['w'])){
?>
var h = <?php echo $h;?>;
var w = <?php echo $w;?>;
var columnWidth = <?php echo $columnWidth;?>;
<?php } ?>

function toggleLoader(){
	if(!loaderShowing){
		$('loader').removeClassName('backgroundLoadHide');
		$('loader').addClassName('backgroundLoadShow');
		$('loader').innerHTML ='<img src="images/ajax-loader.gif"/>';
	}else{
		$('loader').removeClassName('backgroundLoadShow');
		$('loader').addClassName('backgroundLoadHide');
		$('loader').innerHTML ='';
	}
	loaderShowing=!loaderShowing;
	requestPending=!requestPending;
}

function mouseInLink(linkId, index)
{
	$(linkId).removeClassName('mainLink');
	$(linkId).addClassName('mainLinkOver');
	
	if(index == 0){
		movieMouseIn();
	}else if(index == 1){
		tvShowsMouseIn();
	}else if(index == 2){
		musicMouseIn();
	}
}

function mouseOutLink(linkId, index)
{
	$(linkId).removeClassName('mainLinkOver');
	$(linkId).addClassName('mainLink');
}

function showWeatherOverlay(){
	$('weatherOverlay').removeClassName('weatherOverlayHidden');
	$('weatherOverlay').addClassName('weatherOverlay');
	showForcast();
}

function hideWeatherOverlay(){
	$('weatherOverlay').removeClassName('weatherOverlay');
	$('weatherOverlay').addClassName('weatherOverlayHidden');
}

function showForcast() {
	new Ajax.Request('scripts/showWeatherForcast.php?uId='+getUniqueCode(),
	  {
		method:'get',
		onSuccess: function(transport){
			var forcastHtml = transport.responseText;
			$('weatherOverlay').innerHTML = forcastHtml;
		},
		onFailure: function(){ alert('error getting forcast...') }
	  });
}

function showRandomModal(){
	showConfirmWindow('Random Videos', '<CENTER>Loading...</CENTER>', 750, 1000, false);
}

function movieMouseIn() {
	if(!requestPending){
		toggleLoader();
		
		new Ajax.Request('scripts/getRandomMovieBackground.php?uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				document.body.background = json.backdropSrc;
				toggleLoader();
				
			},
			onFailure: function(){ alert('error on movie mouse in...');toggleLoader(); }
		  });
	  }
}

function tvShowsMouseIn() {
	if(!requestPending){
		toggleLoader();
		
		new Ajax.Request('scripts/getRandomTvShowBackground.php?uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				document.body.background = json.backdropSrc;
				toggleLoader();
			},
			onFailure: function(){ alert('error on tv show mouse in...');toggleLoader(); }
		  });
	}
}

function musicMouseIn() {
	if(!requestPending){
		document.body.background = 'images/music_bg.jpg';
	}
}

function weatherMouseIn() {
	showWeatherOverlay();
	
	if(!requestPending){
		toggleLoader();
		
		new Ajax.Request('scripts/getRandomWeatherBackground.php?uId='+getUniqueCode()+'&iconName='+$('weatherIcon').readAttribute('iconName'),
		  {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				document.body.background = json.backdropSrc;
				toggleLoader();
				
			},
			onFailure: function(){ alert('error on movie mouse in...');toggleLoader(); }
		  });
	 }
}

function randomMouseIn() {
	document.body.background = 'images/random_bg.jpg';
//	if(!requestPending){
//		document.body.background = 'images/music_bg.jpg';
//	}
}

function initializeApplication(){
	movieMouseIn();
	sendWakeOnLanPackets();
	setTimeout("turnOffSplashScreen();", 3000);
}

function sendWakeOnLanPackets(){
	new Ajax.Request('scripts/wolScript.php?uId='+getUniqueCode(),
		  {
			method:'get',
			onSuccess: function(transport){
				if(transport.responseText != ''){
					alert('Error sending wake on lan packets');
				}
			},
			onFailure: function(){ alert('Error sending wake on lan packets.');}
		  });
}

function turnOffSplashScreen(){
	$('titleScreen').removeClassName('titleScreenBack');
	$('titleScreen').addClassName('titleScreenBackHidden');
	
	new Ajax.Request('scripts/endSplashScreen.php?uId='+getUniqueCode(),
		  {
			method:'get'
		  });
}
</script>
</head>
<body bgcolor="#B3B3B3" style="overflow:hidden;" background="" onLoad="<?php if($showSplash){ echo 'initializeApplication();';}else{ echo 'movieMouseIn();';}?>clock();">

<!-- redirect script -->
<?php if(!isset($_GET['uId']) || !isset($_GET['w']) || !isset($_GET['h'])){ ?>
	<script type="text/javascript">
		//ps3 will cache if we dont add a unique timestamp at the end of all urls
		window.location = 'index.php?w='+window.document.body.clientWidth+'&h='+window.document.body.clientHeight+'&uId='+getUniqueCode();
	</script>
<?php } ?>

<!--spash screen -->
<?php if($showSplash){ ?>
<div id="titleScreen" class="titleScreenBack">
<div style="height:400px;"></div>
<img src="images/titleLogo.png"/>
<br/>
<div style="height:250px;"></div>
<img src="images/ajax-loadermainscreen.gif"/>
</div>
<?php } ?>

<div class="wrapper">

<!-- right info panel -->
<?php include('/includes/topRightInfoPanel.php'); ?>

<!-- menu background -->
<div id="navBg" style="position: absolute;top: 55%; width:100%; height:225px; background:url(images/homemenu_back.png) no-repeat;">
</div>

<!-- nav menu -->
<div id="navMenu" style="position: absolute; top:601px; width:<?php echo mysql_num_rows($themesResult)*$columnWidth;?>px;">
<table height="100" width="<?php echo mysql_num_rows($themesResult)*$columnWidth;?>" cellpadding="0" cellspacing="0" style="margin-left:<?php echo $columnWidth/2;?>px;">
<tr>
<?php
$i=0;
while ($row = mysql_fetch_array($themesResult)) {
	$name = strtoupper ($row['type']);
	$url = $row['url'];
	
	if($url != ''){
		$url.='?&w='.$w.'&h='.$h.'&uId='.time();
	}
?>
	<td id="menuTD<?php echo $i;?>" link="<?php echo $url;?>" typeId="<?php echo $row['type_id'];?>" class="mainLink<?php if($i==0){echo 'Over';}?>" style="text-align:center;" width="<?php echo $columnWidth;?>"><?php echo $name;?></td>
<?php
	$i++;
}
?>
</tr>
</table>
</div>

<!-- weather overlay -->
<div id="weatherOverlay" class="weatherOverlayHidden">
</div>

<!-- loader div -->
<div id="loader" align="center" style="position: absolute;top: 75%;width:100%;">
</div>
            <div class="push"></div>
</div>

<!-- footer -->
<?php include('/includes/footerPanel.php'); ?>
		
<!-- Modal Window Code -->
<?php include('/includes/modalWindow.php'); ?>
</body>
</html>